package cn.sccdlg.es.service;

import cn.sccdlg.es.dao.ItemRepository;
import cn.sccdlg.es.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/12/4 21:25
 * ES回顾商品业务层实现类
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemRepository itemRepository;

    public void save(Item item) {
        this.itemRepository.save(item);
    }

    public void delete(Item item) {
        this.itemRepository.delete(item);
    }

    public void saveAll(List<Item> items) {
        this.itemRepository.saveAll(items);
    }

    public Iterable<Item> findAll() {
        Iterable<Item> items = this.itemRepository.findAll();
        return items;
    }

    public Page<Item> findAllPage(Integer page, Integer rows) {
        Page<Item> result = this.itemRepository.findAll(PageRequest.of(page,rows));
        return result;
    }

    public List<Item> findByTitleAndContent(String title, String content) {
        List<Item> list = this.itemRepository.findByTitleAndContent(title, content);
        return list;
    }

    public Page<Item> findByTitleOrContent(String title, String content, Integer page, Integer rows) {
        Page<Item> pages = this.itemRepository.findByTitleOrContent(title, content, PageRequest.of(page, rows));
        return pages;
    }

    public Page<Item> findByTitleAndContentAndIdBetween(String title, String Content, Long min, Long max, Integer page,
                                                        Integer rows) {
        Page<Item> items = this.itemRepository.findByTitleAndContentAndIdBetween(title, Content, min, max,
                PageRequest.of(page, rows));
        return items;
    }

}
